package com.ihealthway.all.groupbuy.modules.admin;

import com.ihealthway.all.commons.Constants;
import com.ihealthway.all.utils.Utils;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * UM Editor uploader module
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean
@At("/admin/umeditor")
public class UMEditorModule {

    private static final Log logger = Logs.get();

    @Inject
    private PropertiesProxy props;

    private List<String> allowTypes = Arrays.asList("jpg", "jpeg", "bmp", "png", "git");

    @At
    @AdaptBy(type = UploadAdaptor.class, args = { "ioc:uploadImg" })
    public UMEditorUpResponse uploadImg(@Param("..") NutMap nm) {

        UMEditorUpResponse umUpResp = new UMEditorUpResponse();
        TempFile upTempFile = ((TempFile) nm.get("upfile"));
        if (checkFileType(upTempFile.getSubmittedFileName())) {
            File upFile = upTempFile.getFile();
            String newImgName = Utils.renameUploadFile(upFile.getName());
            String savePath = props.get(Constants.UPLOAD_ATTACHMENT_SAVE_PATH) +
                    props.get(Constants.UM_ATTACHMENT_IMG_FOLDER);

            String imgPath = savePath + "/" + newImgName;
            String imgURL = Constants.UM_AT_CTX_PATH + newImgName;

            try {
                Files.copyFile(upFile, new File(imgPath));
            } catch (IOException e) {
                logger.error("Upload um editor image failed!!!", e);
                umUpResp.setState("上传文件时拷贝失败!");
            }

            umUpResp.setName(umUpResp.getName());
            umUpResp.setOriginalName(upTempFile.getSubmittedFileName());
            umUpResp.setState("SUCCESS");
            umUpResp.setUrl(imgURL);
            umUpResp.setSize(upFile.length());
            umUpResp.setType(upTempFile.getSubmittedFileName().substring(upTempFile.getSubmittedFileName().lastIndexOf(".")+1));
        } else {
            umUpResp.setState("上传的不图片文件,请重新选择!");
        }

        return umUpResp;
    }

    private boolean checkFileType(String fileName) {
        long count = allowTypes.stream().filter(type -> fileName.toLowerCase().endsWith(type)).count();
        return count > 0l;
    }


    private class UMEditorUpResponse {

        private String name;
        private String originalName;
        private String type;
        private String url;
        private Long size;
        private String state = "SUCCESS";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getOriginalName() {
            return originalName;
        }

        public void setOriginalName(String originalName) {
            this.originalName = originalName;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public Long getSize() {
            return size;
        }

        public void setSize(Long size) {
            this.size = size;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        @Override
        public String toString() {
            return "UMEditorUpResponse{" +
                    "name='" + name + '\'' +
                    ", originalName='" + originalName + '\'' +
                    ", type='" + type + '\'' +
                    ", url='" + url + '\'' +
                    ", size=" + size +
                    ", state='" + state + '\'' +
                    '}';
        }
    }

}
